package com.lord.otter.component.infrastructure.retry;

import com.lord.otter.component.context.spring.StaticSpringContext;
import com.lord.otter.component.infrastructure.retry.executor.HttpExecutorFacade;
import com.lord.otter.component.infrastructure.retry.repository.HttpRetryTaskRepository;
import com.lord.otter.component.infrastructure.retry.strategy.AbstractExceptionHandler;
import org.springframework.http.HttpHeaders;

/**
 * http请求重试客户端 工厂
 *
 * @author lord
 * 2025/8/29 17:06
 */
public class HttpRetryClientFactory {
    /**
     * 异常处理策略
     */
    private static final AbstractExceptionHandler exceptionHandler;
    /**
     * 请求执行
     */
    private static final HttpExecutorFacade executor;
    /**
     * HttpRetryTask仓储接口
     */
    private static final HttpRetryTaskRepository taskRepository;

    static {
        exceptionHandler = StaticSpringContext.getBean(AbstractExceptionHandler.class);
        executor = StaticSpringContext.getBean(HttpExecutorFacade.class);
        taskRepository = StaticSpringContext.getBean(HttpRetryTaskRepository.class);
    }

    public static HttpRetryClient defaultClient(
            HttpRetryBusinessGroup businessGroup,
            String businessId,
            String url
    ) {
        return defaultClient(businessGroup, businessId, url, null, null, null);
    }

    public static HttpRetryClient defaultClient(
            HttpRetryBusinessGroup businessGroup,
            String businessId,
            String url,
            HttpHeaders httpHeaders
    ) {
        return defaultClient(businessGroup, businessId, url, null, httpHeaders, null);
    }

    public static HttpRetryClient defaultClient(
            HttpRetryBusinessGroup businessGroup,
            String businessId,
            String url,
            String callbackBeanName
    ) {
        return defaultClient(businessGroup, businessId, url, callbackBeanName, null, null);
    }

    public static HttpRetryClient defaultClient(
            HttpRetryBusinessGroup businessGroup,
            String businessId,
            String url,
            String callbackBeanName,
            HttpHeaders httpHeaders
    ) {
        return defaultClient(businessGroup, businessId, url, callbackBeanName, httpHeaders, null);
    }

    /**
     * @param businessGroup    业务组 - 用于区别业务
     * @param businessId       业务主键
     * @param callbackBeanName 成功回调的bean名称
     * @param url              请求地址
     * @param httpHeaders      请求头
     * @param requestBody      请求体
     * @author lord
     * 2025/8/29 17:18
     */
    public static HttpRetryClient defaultClient(
            HttpRetryBusinessGroup businessGroup,
            String businessId,
            String url,
            String callbackBeanName,
            HttpHeaders httpHeaders,
            String requestBody
    ) {
        return HttpRetryClient.builder()
                .exceptionHandler(exceptionHandler)
                .executor(executor)
                .taskRepository(taskRepository)
                .businessGroup(businessGroup)
                .businessId(businessId)
                .url(url)
                .callbackBeanName(callbackBeanName)
                .httpHeaders(httpHeaders)
                .requestBody(requestBody)
                .build();
    }
}
